//
//  RBMyCouponListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/3.
//

#import "RBMyCouponListVC.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBMyCouponListCell.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"
#import "RBGoodsDetailVC.h"
#import "RBShopDetailVC.h"
#import "RBSearchResultVC.h"

@interface RBMyCouponListVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong) RBMyCouponResponse *response;

@end

@implementation RBMyCouponListVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(30), 0, Zoom(30), 0);
        group.minimumLineSpacing = Zoom(20);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
    
}

- (void)appendData
{
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.firstObject;
        (NSMutableArray *)group.cellModels;
    });
    for (id model in self.response.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBMyCouponListCell.class);
            cellModel.itemSize = CGSizeMake(kRBMainScreenWidth, Zoom(88));
            cellModel.cellData = model;
            cellModel.eventBlock1 = ^(NSInteger type, NSInteger index, RBMyCouponModel *data) {
                if ([data.coupon_type isEqualToString:@"1"]) {
                    weak_self.tabBarController.selectedIndex = 1;
                    [weak_self.navigationController popToRootViewControllerAnimated:YES];
                } else if ([data.coupon_type isEqualToString:@"2"]) {
                    RBShopDetailVC *vc = RBShopDetailVC.new;
                    vc.id = data.url_id;
                    [weak_self.navigationController pushViewController:vc animated:YES];
                } else if ([data.coupon_type isEqualToString:@"4"]) {
                    RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
                    vc.id = data.url_id;
                    [weak_self.navigationController pushViewController:vc animated:YES];
                } else if ([data.coupon_type isEqualToString:@"5"]) {
                    RBSearchResultVC *vc = RBSearchResultVC.new;
                    vc.keyword = @"";
                    vc.class_id = data.url_id;
                    [self.navigationController pushViewController:vc animated:NO];
                }
            };
            cellModel;
        })];
    }
    [self.collectionView reloadData];
    if(!arrayM.count)
    {
        [self.listView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}


#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"status"] = @(self.type);
    return dicM;
}
- (NSString *)listRequestPath
{
    return @"hu_my_coupon";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBMyCouponResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}


#pragma mark - Event

#pragma mark - Setter

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Getter

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
